/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
class Solution {
public:
    ListNode* reverseList(ListNode* head) {
        ListNode *newHead = nullptr, *cur = head;
        while(cur)
        {
            ListNode *curNext = cur->next;
            cur->next = newHead; // 头插
            newHead = cur; // 往后走
            cur = curNext;
        }
        return newHead;

        /*// 用循环迭代更好，但写下递归；
        // 链表看成一颗树，遇到空结点/叶子结点就返回，让叶子结点指回去
        if(head == nullptr || head->next == nullptr)
            return head;

        ListNode* newHead = reverseList(head->next); // 把head后面的都递归好
        head->next->next = head; // 让叶子结点指回去
        head->next = nullptr; // 为了统一步骤
        return newHead;*/
    }
};